//
//  TeacherData.m
//  ZHZtest
//
//  Created by 李彦宏 on 2017/12/22.
//  Copyright © 2017年 LYH. All rights reserved.
//

#import "TeacherData.h"
#import "TeacherDataCell.h"
#import "TeacherDatamodel.h"//教师模型
#import "JWUserModel.h"//总资料模型
#import "JWUserManager.h"//管理类
#import "BRPickerView.h"

#define TeacherDataCellID @"TeacherDataCell"
@interface TeacherData ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITableView  *TeacherDataTable;
@property (nonatomic, strong) JWUserModel *model;
@property (nonatomic, strong) UIButton *rightBtnitme;

/**
 *教师基本资料
 */
@property (nonatomic, strong) NSMutableArray *TeacherArry;
@end

@implementation TeacherData
/**
 * 基本资料的懒加载
 */
- (NSMutableArray *)TeacherArry
{
    if (_TeacherArry == nil) {
        // 初始化
        // 1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"JWTeacherList.plist" ofType:nil];
        
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *tgArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            // 3.1.创建模型对象
            TeacherDatamodel *tg = [TeacherDatamodel TeacherWhithDic:dict];
            
            // 3.2.添加模型对象到数组中
            [tgArray addObject:tg];
        }
        
        // 4.赋值
        _TeacherArry = tgArray;
    }
    return _TeacherArry;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"基本资料";
    [self setupTable];
    
    _model=[JWUserManager me];
    //rightNavBtn
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     self.rightBtnitme=rightBtn; rightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    [rightBtn setFrame:CGRectMake(0, 0, 44, 44)];
    [rightBtn setTitle:@"修改" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    // Do any additional setup after loading the view.
}
-(void)rightBtnClick:(UIButton *)send{
if([send.titleLabel.text isEqualToString:@"修改"]){
    //    NSLog(@"你可以修改啦");
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.TeacherDataTable reloadData];
        
        [send setTitle:@"保存" forState:UIControlStateNormal];
        send.titleLabel.font=[UIFont systemFontOfSize:20];
        [send setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    });
    
}else{
    if(self.model.sex==nil){
        self.model.sex=@"";
    }else if (self.model.qnumber==nil){
        self.model.qnumber=@"";
    }else if (self.model.wnumber==nil){
        self.model.wnumber=@"";
    }else if (self.model.birthday==nil){
        self.model.birthday=@"";
    }
    
    NSDictionary *dict = @{
                           @"jwid" : JWid,
                           @"jwpassword" :JWpassword,
                           @"name" : self.model.name,
                           @"phone" : self.model.phone,
                           @"sex" : self.model.sex,
                           @"qnumber" : self.model.qnumber,
                           @"wnumber" : self.model.wnumber,
                           @"birthday" : self.model.birthday,
                           };
    //       NSLog(@"--dict--%@", dict);
    
    [[AFNetworkTool shareManager]postJSONWithUrl:JWteacher_Change parmas:dict successData:^(id json) {
        // NSLog(@"--json--%@", json);
        //刷新很重要
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSDictionary *dict = @{
                                   @"phone":[[NSUserDefaults standardUserDefaults] objectForKey:@"rememberTel"],
                                   @"password":[[NSUserDefaults standardUserDefaults] objectForKey:@"rememberpassword"],
                                   @"type":@"3",
                                   };
            
            [[AFNetworkTool shareManager]postJSONWithUrl:Login parmas:dict successData:^(id json) {
                
                //              NSLog(@"-教务-json--%@", json);
                
                self.model = [JWUserModel yy_modelWithJSON:json];
                [JWUserManager save:self.model];
                
            } failure:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            [self.navigationController popViewControllerAnimated:YES];
        });
        
        [MBProgressHUD showSuccess:@"保存成功"];
        
    } failure:^(NSError *error) {
        NSLog(@"--err--%@", error);
    }];
}

}

//UItableView初始化
- (void)setupTable
{
   _TeacherDataTable=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _TeacherDataTable.tableHeaderView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, 0.00001f)];
    _TeacherDataTable.backgroundColor=[UIColor whiteColor];
    _TeacherDataTable.showsHorizontalScrollIndicator=NO;
    _TeacherDataTable.showsVerticalScrollIndicator=NO;
    _TeacherDataTable.dataSource=self;
    _TeacherDataTable.delegate=self;
    _TeacherDataTable.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.TeacherDataTable];
    
    [_TeacherDataTable registerNib:[UINib nibWithNibName:@"TeacherDataCell" bundle:nil] forCellReuseIdentifier:TeacherDataCellID];


}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.TeacherArry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}
#pragma mark - 点击事件
-(void)tapGesture:( UITapGestureRecognizer *)sender{
    [self.view endEditing:YES];
    [BRStringPickerView showStringPickerWithTitle:@"性别" dataSource:@[@"男", @"女"] defaultSelValue:@"男" isAutoSelect:YES resultBlock:^(id selectValue) {
        //        NSLog(@"%@", selectValue);
        if ([selectValue isEqualToString:@"男"]) {
            self.model.sex = @"2";
            //            NSLog(@">>>>>>>>%@",self.model.sex);
        }else{
            self.model.sex = @"1";
        }
        [self.TeacherDataTable reloadData];
    }];
}
-(void)tapGesture2:( UITapGestureRecognizer *)sender{
    [self.view endEditing:YES];
    [BRDatePickerView showDatePickerWithTitle:@"出生年月" dateType:UIDatePickerModeDate defaultSelValue:@"" minDateStr:@"" maxDateStr:@"" isAutoSelect:YES resultBlock:^(NSString *selectValue) {
        self.model.birthday = selectValue;
        [self.TeacherDataTable reloadData];
    }];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TeacherDataCell *cell = [tableView dequeueReusableCellWithIdentifier:TeacherDataCellID];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    TeacherDatamodel *teacherinfo=_TeacherArry[indexPath.row];
    cell.imagecell.image=[UIImage imageNamed:teacherinfo.image];
    cell.listcell.text=teacherinfo.listTeax;
   
    //教务信息
    NSInteger row=indexPath.row;
     cell.JWinfofield.tag=row;
    cell.JWinfofield.delegate=self;
    if([self.rightBtnitme.titleLabel.text isEqualToString:@"修改"]){
        cell.JWinfofield.enabled=NO;
    }else{
        cell.JWinfofield.enabled=YES;
        [cell.JWinfofield addTarget:self action:@selector(textfieldACtion:) forControlEvents:UIControlEventEditingChanged];
    }
    //top事件
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTouchesRequired = 1; //手指数
    tap.numberOfTapsRequired = 1; //tap次数
    tap.delegate= self;
    UITapGestureRecognizer *tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture2:)];
    tap2.numberOfTouchesRequired = 1; //手指数
    tap2.numberOfTapsRequired = 1; //tap次数
    tap2.delegate= self;

    
    switch (row) {
        case 0:
            cell.JWinfofield.text=self.model.name;
            break;
        case 1:
            cell.JWinfofield.text=self.model.phone;
            break;
        case 2:
        {
            if ([self.model.sex isEqualToString:@"1"]) {
                cell.JWinfofield.text = @"女";
            }else{
                cell.JWinfofield.text = @"男";
            }
             [cell.JWinfofield addGestureRecognizer:tap];
            break;
            
        }
        case 3:
            cell.JWinfofield.text=self.model.qnumber;
            break;
        case 4:
            cell.JWinfofield.text=self.model.wnumber;
            break;
        case 5:
            cell.JWinfofield.text=self.model.birthday;
            [cell.JWinfofield addGestureRecognizer:tap2];
            break;
        default:
            break;
    }
    return cell;
}
#pragma -mark —————文本编辑响应事件————————————————————
-(void)textfieldACtion:(UITextField *)textfield{
    switch (textfield.tag) {
        case 0:
            self.model.name=textfield.text;
            break;
        
        case 1:
            self.model.phone=textfield.text;
            break;
        case 2:
//            self.model.sex=textfield.text;
            break;
        case 3:
            self.model.qnumber=textfield.text;
            break;
        case 4:
            self.model.wnumber=textfield.text;
            break;
        case 5:
//            self.model.birthday=textfield.text;
            
            break;
            
        default:
            break;
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.TeacherDataTable deselectRowAtIndexPath:indexPath animated:YES];
    
    [MBProgressHUD showSuccess:@"修改资料点击修改按钮"];
    //    UIViewController *vc = [[UIViewController alloc] init];
    //    vc.view.backgroundColor = [UIColor blueColor];
    //    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
